package io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

public class demo13 {
    public static void main(String[] args) {
        //拷贝文件
        //输出源文件路径和目标文件路径
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入源文件路径");
        String src = scanner.nextLine();
        System.out.println("请输入目标文件路径");
        String destPath = scanner.next();
        //2.判定源文件是否存在
        File srcFile = new File(src);
        if (!srcFile.exists()) {
            System.out.println("源文件不存在");
            return;
        }
        //3.判定目标文件的父目录是否存在
        File destFile = new File(destPath);
        if (!srcFile.isFile()) {
            System.out.println("源文件的父目录不存在");
            return;
        }

        //4.进行拷贝
        copy(srcFile, destFile);
        
    }

    private static void copy(File srcFile, File destFile) {
        try(InputStream inputStream = new FileInputStream(srcFile);
        OutputStream outputStream = new FileOutputStream(destFile)){

            while (true) {
                byte[] buffer = new byte[1024];
                int len = inputStream.read(buffer);
                //System.out.println("len = "+ len);
                if (len == -1) {
                    break;
                }
                outputStream.write(buffer,0,len);
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
